"""
pytest配置文件和fixtures
"""
import pytest
import json
import os
from api.user_api import UserAPI
from config.config import APIConfig


@pytest.fixture(scope="session")
def test_data():
    """加载测试数据"""
    data_file = os.path.join(os.path.dirname(__file__), "..", "data", "users_data.json")
    with open(data_file, 'r', encoding='utf-8') as f:
        return json.load(f)


@pytest.fixture(scope="class")
def user_api():
    """用户API fixture"""
    config = APIConfig.get_config()
    api_key = config.get("api_key", "<token>")
    
    # 如果是默认的token，跳过测试
    if api_key == "<token>":
        pytest.skip("需要设置有效的API密钥 (GLM_API_KEY环境变量)")
        
    return UserAPI(api_key)


@pytest.fixture(scope="function")
def api_client():
    """API客户端fixture"""
    config = APIConfig.get_config()
    api_key = config.get("api_key", "<token>")
    
    # 如果是默认的token，跳过测试
    if api_key == "<token>":
        pytest.skip("需要设置有效的API密钥 (GLM_API_KEY环境变量)")
        
    from utils.api_helper import GLMAPIClient
    return GLMAPIClient(
        api_key=api_key,
        base_url=config.get("base_url"),
        timeout=config.get("timeout", 60)
    )